package rust;

import org.junit.Test;
import sf.database.template.sql.SqlHelp;
import sf.tools.IOUtils;
import sf.tools.StringUtils;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class C2Rust {
    public static void main(String[] args) {
        String sourceFile = "E:\\workspace-git\\intelligence\\cloud\\jni\\rust\\aiflow\\src\\lib_header\\app_inc\\AIOP\\aiflow_generate_cfg.h";
        String outputFile = "D:/bakup/out.txt";
        List<String> lines = new ArrayList<>();
        List<String> results = new ArrayList<>();
        try {
            IOUtils.asStringLambda(new FileInputStream(sourceFile), StandardCharsets.UTF_8, true, line -> {
                lines.add(line);
                return true;
            });
            boolean startStruct = false;
            for (String line : lines) {
                String s = line.trim();
                if (s.startsWith("//") || s.startsWith("/*") || s.startsWith("*")) {
                    results.add(s);
                    continue;
                }
                if (s.contains("struct") || s.contains("union")) {
                    startStruct = true;
                }
                if (s.contains("typedef")) {
                    line = line.replace("typedef", "pub");
                    results.add("#[repr(C)]");
                    results.add(line);
                    continue;
                }
                if ("{".equals(s)) {
                    results.add(line);
                    continue;
                }
                if (s.contains("}")) {
                    startStruct = false;
                    results.add(line);
                    continue;
                }
                if (startStruct) {
                    StringBuilder sb = new StringBuilder();
                    s = SqlHelp.replaceLineSql(s);
                    for (int i = 0; i < s.length(); i++) {
                        char c = s.charAt(i);
                        sb.append(c);
                        if (c == '*') {
                            sb.append(' ');
                        }
                    }
                    s = sb.toString();
                    sb.delete(0, sb.length());
                    if (StringUtils.isNotBlank(s.trim())) {
                        String[] subArr = s.split(" ");
                        if (subArr.length > 0) {
                            sb.append(" pub ");
                            boolean isFieldName = false;
                            for (int i = 0; i < subArr.length; i++) {
                                String sub = subArr[i];
                                if (sub.endsWith(";")) {
                                    isFieldName = true;
                                    if (sub.contains("[")) {
                                        sub = sub.replace(";", "");
                                        String[] tempArr = org.apache.commons.lang3.StringUtils.split(sub, "[", 2);
                                        sb.append(tempArr[0]);
                                        sb.append(":[");
                                        boolean f = false;
                                        for (int j = 0; j < i; j++) {
                                            if (f) {
                                                sb.append(' ');
                                            }
                                            sb.append(subArr[j]);
                                            f = true;
                                        }
                                        sb.append(";").append(tempArr[1]);
                                    } else {
                                        sb.append(sub.replace(";", "")).append(":");
                                        boolean f = false;
                                        for (int j = 0; j < i; j++) {
                                            if (f) {
                                                sb.append(' ');
                                            }
                                            sb.append(subArr[j]);
                                            f = true;
                                        }
                                    }
                                } else if (sub.startsWith("//")) {
                                    sb.append(',').append(sub);
                                } else if (!sub.endsWith(";") && isFieldName) {
                                    sb.append(sub).append(' ');
                                }
                            }
//                            for (int i = 0; i < subArr.length; i++) {
//                                String sub = subArr[i];
//                                if (sub.startsWith("//")) {
//                                    sb.append(',');
//                                }
//                                if (!sub.endsWith(";")) {
//                                    sb.append(sub).append(' ');
//                                }
//                            }
                            String str = sb.toString()
                                    .replace("void *", "*mut c_void")
                                    .replace("unsigned char", "u8")
                                    .replace("unsigned short", "u16")
                                    .replace("unsigned int", "u32")
                                    .replace("float", "f32")
                                    .replace("double", "f64")
                                    .replace(" char", " i8")
                                    .replace(" short", " i16")
                                    .replace(" int", " i32");
                            results.add(str);
                        }
                    } else {
                        results.add(s);
                    }
                } else {
                    results.add(line);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile, false)))) {
            for (String line : results) {
                bw.newLine();
                bw.write(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void t() {
        String str = "a b c";
        System.out.println(Arrays.asList(str.split(" ")));
    }
}
